package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2024/9/2 10:32
 * @apiNote
 */

public class Test02024MaxConsecutiveAnswers {
    public static void main(String[] args) {
        String answerKey = "TTFTTFTT";
        int k = 2;
        System.out.println(maxConsecutiveAnswers(answerKey, k));
    }
    public static int maxConsecutiveAnswers(String answerKey, int k) {
        return Math.max(maxConsecutiveChar(answerKey, k, 'T'), maxConsecutiveChar(answerKey, k, 'F'));
    }

    public static int maxConsecutiveChar(String answerKey, int k,char ch){
        int n = answerKey.length();
        int ans = 0;
        for(int left = 0, right = 0,sum = 0;right < n;right++){
            sum += answerKey.charAt(right) != ch ? 1 : 0;
            while(sum > k){
                sum -= answerKey.charAt(left++) != ch ? 1:0;
            }
            ans = Math.max(ans,right - left + 1);
        }
        return ans;
    }
}
